package com.yzg.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @author misterWei
 * @create 2020年06月15号:21点08分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.AqsMain")
public class AqsMain {

    public static void main(String[] args) {
        MyLock lock = new MyLock();
        new Thread(()->{
            if (lock.tryLock()) {
                try {
                    // 不可重入锁的体现
                    log.info("获取到锁....");
                    lock.lock();
                    log.info("获取到锁....");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    log.info("释放锁....");
                    lock.unlock();
                }
            }
        },"t1").start();

        new Thread(()->{
                lock.lock();
                try {
                    log.info("获取到锁....");
                }finally {
                    log.info("释放锁....");
                    lock.unlock();
                }
        },"t2").start();

    }
}

// 自定义不可重入锁的实现
class MyLock implements Lock {

    class MySync extends AbstractQueuedSynchronizer {
        /**
         * 获取锁
         *
         * @param arg
         * @return
         */
        @Override
        protected boolean tryAcquire(int arg) {
            if (compareAndSetState(0, 1)) {
                //相当于把 Monitor 设置为当前线程持有
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }

        /**
         * 归还锁
         *
         * @param arg
         * @return
         */
        @Override
        protected boolean tryRelease(int arg) {
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
        }

        /**
         * 判断是否获取锁
         *
         * @return
         */
        @Override
        protected boolean isHeldExclusively() {
            return getState() == 1;
        }


        public Condition newCondition(){
            return new ConditionObject();
        }
    }

    private MySync sync = new MySync();

    /**
     * 锁住当前线程
     */
    @Override
    public void lock() {
        // 修改状态,设置当前线程持有锁
        sync.acquire(1);

    }

    /**
     * 加锁但是能够打断
     *
     * @throws InterruptedException
     */
    @Override
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }

    /**
     * 尝试获取锁
     *
     * @return
     */
    @Override
    public boolean tryLock() {
        return  sync.tryAcquire(1);
    }

    /**
     * 尝试获取锁,但是有超时时间设置
     *
     * @param time
     * @param unit
     * @return
     * @throws InterruptedException
     */
    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return sync.tryAcquireNanos(1,unit.toNanos(time));
    }

    /**
     * 释放锁
     */
    @Override
    public void unlock() {
        sync.release(1);
    }

    /**
     * 得到条件变量
     *
     * @return
     */
    @Override
    public Condition newCondition() {
        return sync.newCondition();
    }
}
